﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-20XX，匠梦科技                                         *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019年5月19日 14:59:28
 *  文件描述:扩展代码
 *****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Util
{
    public static class UtilSys
    {
        public static void SetOrAdd<T1, T2>(this Dictionary<T1, T2> dic, T1 key, T2 v)
        {
            if (dic.ContainsKey(key))
            {
                dic[key] = v;
            }
            else
            {
                dic.Add(key, v);
            }
        }
    }

    public static class UtilUnity
    {
        public static T GetOrAddComponent<T>(this Transform v) where T : Component
        {
            T t = v.GetComponent<T>();
            if (!t)
            {
                t = v.gameObject.AddComponent<T>();
            }
            return t;
        }
    }

    public static class UtilPhysics
    {
    }

    /// <summary>
    /// 执行队列
    /// </summary>
    public class PerformQueue
    {
        private int m_id = 0;
        private readonly List<Action<Action>> m_list = new List<Action<Action>>();
        private readonly Action m_finish;
        private string Name;
        public uint Id;

        public PerformQueue(Action finish = null, string name = null)
        {
            m_finish = finish;
            Name = name;
            Id = debug.Sole;
            if (Name != null)
            {
                debug.log(Id, Name, "Str  PQ");
            }
        }

        public void Add(Action<Action> action)
        {
            m_list.Add(action);
        }

        public void Next()
        {
            try
            {
                if (m_id >= m_list.Count)
                {
                    Over();
                    return;
                }
                m_list[m_id++].Invoke(Next);
            }
            catch (System.Exception ex)
            {
                debug.log(debug.Level.error_3, ex);
            }
        }

        public void Over()
        {
            m_id = 0;
            m_list.Clear();
            if (Name != null)
            {
                debug.log(Id, Name, "End PQ");
            }
            if (m_finish != null)
            {
                m_finish.Invoke();
            }
        }
    }
    public class TimePerformQueue
    {
        public class Node
        {
            public float time;//延迟执行
            public Action action;//执行
            public Node(float time, Action action) { this.time = time; this.action = action; }
        }
        private int m_id = 0;
        private readonly List<Node> m_list = new List<Node>();
        private readonly Action m_finish;
        private string Name;
        public uint Id;

        public TimePerformQueue(Action finish = null, string name = null)
        {
            m_finish = finish;
            Name = name;
            Id = debug.Sole;
            if (Name != null)
            {
                debug.log(Id, Name, "Str  TPQ");
            }
        }
        //延迟时间,执行事件
        public void Add(float time, Action action)
        {
            m_list.Add(new Node(time, action));
        }

        public void Next()
        {
            try
            {
                if (m_list.Count <= 0)
                {
                    Over();
                    return;
                }
                var node = m_list[0];
                m_list.RemoveAt(0);
                node.action.Invoke();
                Timer.Instance.Add(node.time, 1, () =>//执行N秒后，并且开启下一个
                {
                    Next();
                });

            }
            catch (System.Exception ex)
            {
                debug.log(debug.Level.error_3, ex);
            }
        }

        public void Over()
        {
            m_id = 0;
            m_list.Clear();
            if (Name != null)
            {
                debug.log(Id, Name, "End TPQ");
            }
            if (m_finish != null)
            {
                m_finish.Invoke();
            }
        }
    }

    /// <summary>
    /// 计划任务
    /// </summary>
    public class TaskPlan
    {
        private readonly Action m_finish;
        private readonly Dictionary<string, bool> m_dic = new Dictionary<string, bool>();
        private string Name;
        public uint Id;

        public TaskPlan(Action finish = null, string name = null)
        {
            this.m_finish = finish;
            Name = name;
            Id = debug.Sole;
            if (Name != null)
            {
                debug.log(Id, Name, "Str TP");
            }
        }

        public void Add(string name)
        {
            if (!m_dic.ContainsKey(name))
            {
                m_dic.Add(name, false);
            }
        }

        public void Finish(string name)
        {
            //m_dic[name] = true;
            m_dic.Remove(name);
            if (IsAllFinish())
            {
                m_dic.Clear();
                if (Name != null)
                {
                    debug.log(Id, Name, "End TP");
                }
                if (m_finish != null)
                {
                    m_finish.Invoke();
                }
            }
        }

        public bool IsAllFinish()
        {
            if (m_dic.Count > 0)
            {
                return false;
            }
            //List<bool> list = new List<bool>(m_dic.Values);
            //int num = list.Count;
            //for (int i = 0; i < num; i++)
            //{
            //    if (!list[i])
            //    {
            //        return false;
            //    }
            //}
            //var e = m_dic.GetEnumerator();
            //while (e.MoveNext())
            //{
            //    if (!e.Current.Value)
            //    {
            //        return false;
            //    }
            //}
            return true;
        }

        public int IsFinishNum()
        {
            int n = 0;
            n = m_dic.Count;
            //var e = m_dic.GetEnumerator();
            //while (e.MoveNext())
            //{
            //    if (e.Current.Value == false)
            //    {
            //        n++;
            //    }
            //}
            return n;
        }

        public List<string> GetAllKey()
        {
            return new List<string>(m_dic.Keys);
        }
    }
    [Serializable]
    public class Mint
    {
        public int v;
    }
    public class Mlist<T>:List<T>
    {

    }
     
    [System.Serializable]
    public class Mdic<TKey, TValue> : Dictionary<TKey, TValue> 
    { 
    }
}